package com.example.replacebooks.service;

import com.example.replacebooks.entity.Address;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.exception.CustomException;
import com.example.replacebooks.mapper.AddressMapper;
import com.example.replacebooks.mapper.ClassMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressService {
    @Resource
    private AddressMapper addressMapper;

    public List<Address> findAll(){
        return addressMapper.selectAll();
    }

//    管理员查全部地址
    public PageInfo<Address> findBySearch(Params params){
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Address> list = addressMapper.findBySearch(params);
        return PageInfo.of(list);
    }
    //    普通用户查自己的全部地址
    public PageInfo<Address> findById(Params params){
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Address> list = addressMapper.findById(params);
        return PageInfo.of(list);
    }


    public void add(Address address) {
        // 进行重复性判断，同一id的地址替换，不同id新增
        Address addressid = addressMapper.findByAddressid(address.getAddressid());
        if (addressid != null){
            // 该id已存在
            addressMapper.updateByaddressid(address);
        }else{
            addressMapper.insertSelective(address);
        }
    }

    public void delete(Integer addressid) {
        addressMapper.deleteByPrimaryKey(addressid);
    }
}
